package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Slf4j
@Api(tags = "用户地址相关接口")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;
    @PostMapping
    @ApiOperation("增加地址")
    public Result add(@RequestBody AddressBook addressBook){
        addressBookService.add(addressBook);
        return Result.success();
    }


    @GetMapping("/list")
    @ApiOperation("查询用户所有地址")
    public Result<List<AddressBook>> select(){
        return Result.success(addressBookService.findAll());
    }


    @GetMapping("/{id}")
    @ApiOperation("根据地址id查询地址")
    public Result<AddressBook> findByid(@PathVariable Long id){
        return Result.success(addressBookService.findByid(id));
    }

    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result findByid(@RequestBody AddressBook addressBook ){
        addressBookService.setDefault(addressBook);
        return Result.success();
    }

    @GetMapping("/default")
    @ApiOperation("获取默认地址")
    public Result<AddressBook> findDefault(){
        return Result.success(addressBookService.findDefault());
    }

    /**
     * 根据id删除地址
     *
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation("根据id删除地址")
    public Result deleteById(Long id) {
        addressBookService.deleteById(id);
        return Result.success();
    }
    /**
     * 根据id修改地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping
    @ApiOperation("根据id修改地址")
    public Result update(@RequestBody AddressBook addressBook) {
        addressBookService.update(addressBook);
        return Result.success();
    }


}
